
function [vals inds] = randmax(X)
% The same as the regular max function, but if there are ties in max correlation value, 
% this function returns a randomly selected best class, rather than always choosing 
% the class with the smallest class label (as would happen if normal max is used)


    if size(size(X), 2) > 2
        error('max rand only capable right now of dealing with 2D matracies')
    end


    [vals inds] = max(X);
    [sorted_vals sorted_inds] = sort(X, 'descend');

    if ~(length(find(diff(sorted_vals(1:2, :)) == 0)) == 0)

        for iTies = find(diff(sorted_vals(1:2, :)) == 0)
            curr_max_inds = sorted_inds(find(sorted_vals(:, iTies) == sorted_vals(1, iTies)), iTies);   % find all "labels" with max correlation values
            curr_max_inds = curr_max_inds(randperm(length(curr_max_inds)));   % randomly select a "label" from all the ones that have the max correlation value       
            inds(iTies) = curr_max_inds(1);
        end

    end


end